package org.csu.mypetstore.web.servlets;

import com.sun.org.apache.xpath.internal.operations.Or;
import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.domain.Order;
import org.csu.mypetstore.service.LogService;
import org.csu.mypetstore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class NewOrderServlet extends HttpServlet {

    private static final String VIER_ORDER = "/WEB-INF/jsp/order/Checkout.jsp";

    private Order order;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();

        order = new Order();

        //利用account和cart初始化order
        Account account = (Account) session.getAttribute("account");
        Cart cart = (Cart) session.getAttribute("cart");
        order.initOrder(account, cart);

        //对order赋值
        String cardType = request.getParameter("cardType");
        String creditCard = request.getParameter("creditCard");
        String expiryDate = request.getParameter("expiryDate");
        String billToFirstName = request.getParameter("billToFirstName");
        String billToLastName = request.getParameter("billToLastName");
        String billAddress1 = request.getParameter("billAddress1");
        String billAddress2 = request.getParameter("billAddress2");
        String billCity = request.getParameter("billCity");
        String billState = request.getParameter("billState");
        String billZip = request.getParameter("billZip");
        String billCountry = request.getParameter("billCountry");
        String shipToFirstName = request.getParameter("shipToFirstName");
        String shipToLastName = request.getParameter("shipToLastName");
        String shipAddress1 = request.getParameter("shipAddress1");
        String shipAddress2 = request.getParameter("shipAddress2");
        String shipCity = request.getParameter("shipCity");
        String shipState = request.getParameter("shipState");
        String shipZip = request.getParameter("shipZip");
        String shipCountry = request.getParameter("shipCountry");


        if (cardType != null && !cardType.equals("")) {
            order.setCardType(cardType);
        }
        if (creditCard != null && !creditCard.equals("")) {
            order.setCreditCard(creditCard);
        }
        if (expiryDate != null && !expiryDate.equals("")) {
            order.setExpiryDate(expiryDate);
        }

        if (billToFirstName != null && !billToFirstName.equals("")) {
            order.setBillToFirstName(billToFirstName);
        }
        if (billToLastName != null && !billToLastName.equals("")) {
            order.setBillToLastName(billToLastName);
        }
        if (billAddress1 != null && !billAddress1.equals("")) {
            order.setBillAddress1(billAddress1);
        }
        if (billAddress2 != null && !billAddress2.equals("")) {
            order.setBillAddress2(billAddress2);
        }
        if (billCity != null && !billCity.equals("")) {
            order.setBillCity(billCity);
        }
        if (billState != null && !billState.equals("")) {
            order.setBillState(billState);
        }
        if (billZip != null && !billZip.equals("")) {
            order.setBillZip(billZip);
        }
        if (billCountry != null && !billCountry.equals("")) {
            order.setBillCountry(billCountry);
        }

        if (shipToFirstName != null && !shipToFirstName.equals("")) {
            order.setShipToFirstName(shipToFirstName);
        } else {
            order.setShipToFirstName(billToFirstName);
        }
        if (shipToLastName != null && !shipToLastName.equals("")) {
            order.setShipToLastName(shipToLastName);
        } else {
            order.setShipToFirstName(billToLastName);
        }
        if (shipAddress1 != null && !shipAddress1.equals("")) {
            order.setShipAddress1(shipAddress1);
        } else {
            order.setShipAddress1(billAddress1);
        }
        if (shipAddress2 != null && !shipAddress2.equals("")) {
            order.setShipAddress2(shipAddress2);
        } else {
            order.setShipAddress2(billAddress2);
        }
        if (shipCity != null && !shipCity.equals("")) {
            order.setShipCity(shipCity);
        } else {
            order.setShipCity(billCity);
        }
        if (shipState != null && !shipState.equals("")) {
            order.setShipState(shipState);
        } else {
            order.setShipState(billState);
        }
        if (shipZip != null && !shipZip.equals("")) {
            order.setShipZip(shipZip);
        } else {
            order.setShipZip(billZip);
        }
        if (shipCountry != null && !shipCountry.equals("")) {
            order.setShipCountry(shipCountry);
        } else {
            order.setShipCountry(billCountry);
        }


        session.setAttribute("order", order);


        if (order != null) {
            OrderService orderService = new OrderService();

            //将order插入数据库中，其中order的orderId由程序自动增加，无需人为赋值
            orderService.insertOrder(order);

            //清空购物车
            String userid = (String) session.getAttribute("username");
            cart = new Cart();
            cart.removeAllCartItemsByUserid(userid);
            String workingItemId = null;
            session.setAttribute("cart", cart);
            session.setAttribute("workingItemId", workingItemId);


            String message = "Thank you, your order has been submitted.";
            session.setAttribute("message", message);

            request.getRequestDispatcher(VIER_ORDER).forward(request, response);
        }
    }
}
